package com.hqyj.springboot.modules.test.controller;

import com.hqyj.springboot.modules.common.vo.Result;
import com.hqyj.springboot.modules.common.vo.Search;
import com.hqyj.springboot.modules.test.entity.Student;
import com.hqyj.springboot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 127.0.0.1/api/student ----post
     * {"studentName":"佚名"}
     * {"studentName":"佚名","studentCard":{"cardNum":"20220818001"}}
     * @param student
     * @return
     */
    @PostMapping(value = "/student",consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    /**
     * 127.0.0.1/api/student ----post
     * {"id":1,"studentName":"张三"}
     * {"id":1,"studentName":"佚名","studentCard":{"cardNum":"20220818001"}}
     *
     * @param student
     * @return
     */
    @PutMapping (value = "/student", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }

    @DeleteMapping (value = "/student/{id}")
    public Result<Object> deleteStudentById(@PathVariable int id){
       return studentService.deleteStudentById(id);
    }

    /**
     * 127.0.0.1/api/student/6
     * @param id
     * @return
     */
    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable int id){
        return studentService.getStudentById(id);
    }


    /**
     * 12.0.0.1/api/students ---- post
     * {"currentPage":1,"pageSize":2,"sort":"studentName","direction":"desc","keyword":""}
     * @param search
     * @return
     */
    @PostMapping(value = "/students",consumes = "application/json")
    public Page getStudentsBySearch(@RequestBody Search search){
        return studentService.getStudentsBySearch(search);
    }

    /**
     * 127.0.0.1/students?studentName
     * @param studentName
     * @return
     */
    @GetMapping("/students")
    public List<Student> getStudentsByStudentName(@RequestParam String studentName){
        return studentService.getStudentsByStudentName(studentName);
    }
}
